function [BW, Y, X, Z] = detect_beads_LF(LF)

%这段代码的核心是利用高斯差分（DoG）来增强三维图像中的珠状结构，并结合局部最小值检测来定位这些结构。
% 它的实现包括预处理、区域掩码设定、多尺度模糊和局部特征提取等步骤，适合于检测图像中的亮度突出、呈点状的目标物。
close all;
load('./run/LF.mat','LF');

LF = double(LF);
k = [1.4, 1.4, 1.4];
sigma = [15,15,15];

blur1 = imgaussfilt3(LF, sigma);
BW = zeros(size(LF));
BW(5:end-5, 5:end-5, 2:end-2) = 1;
BW(LF<mean(LF(:))) = 0;

for scale = 1:1
    sigma = k.^scale .* sigma;
    blur2 = imgaussfilt3(LF, sigma);
    DoG = blur2 - blur1;
    BW = BW & imregionalmin(DoG);
    blur1 = blur2;
end

I = find(BW ~= 0);
[Y, X, Z] = ind2sub(size(BW),I);


xx_lf = X';
yy_lf = Y';
zz_lf = Z';

% centroids_subpixel = detect_beads_LF_2(LF-100);
% X_LF = centroids_subpixel(:, 1);
% Y_LF = centroids_subpixel(:, 2);
% Z_LF = centroids_subpixel(:, 3);
% xx_lf = X_LF';
% yy_lf = Y_LF';
% zz_lf = Z_LF';

figure, imshow(imadjust(max(LF, [], 3))),title('LF beads xy');
for i = 1:size(yy_lf,2)
    hold on, plot(xx_lf(i), yy_lf(i), 'o', 'MarkerSize', 5);
end
figure, imshow(imadjust(squeeze(max(LF, [], 2)))), title('LF beads yz');
for i = 1:size(yy_lf,2)
    hold on, plot(zz_lf(i), yy_lf(i), 'o', 'MarkerSize', 3);
end


end

